package chatsystem.util;

import java.security.InvalidParameterException;
import java.util.HashMap;

/** Non-thread safe, bidirectional map */
public class BidiMap <Key, Value>
{
   public BidiMap()
   {
      _fwd = new HashMap<Key, Value>();
      _rev = new HashMap<Value, Key>();
   }
   
   public void insert(Key k, Value v)
   {
      if (hasKey(k) || hasValue(v))
         throw new InvalidParameterException("Element already exists: "+k+" or "+v);

      _fwd.put(k, v);
      _rev.put(v, k);
   }
   
   public boolean hasKey(Key k)
   {
      return _fwd.containsKey(k);
   }
   
   public boolean hasValue(Value v)
   {
      return _rev.containsKey(v);
   }
   
   public Value getByKey(Key k)
   {
      return _fwd.get(k);
   }
      
   public Key getByValue(Value v)
   {
      return _rev.get(v);
   }
         
   public void removeByKey(Key k)
   {
      Value v = _fwd.remove(k);
      _rev.remove(v);
   }
            
   public void removeByValue(Value v)
   {
      Key k = _rev.remove(v);
      _fwd.remove(k);
   }
   
   private HashMap<Key, Value> _fwd;
   private HashMap<Value, Key> _rev;
}
